﻿#region File and License Information
/*
<File>
	<License Type="BSD">
		Copyright © 2009 - 2012, Daniel Vaughan. All rights reserved.
	
		This file is part of Calcium (http://calciumsdk.net).

		Redistribution and use in source and binary forms, with or without
		modification, are permitted provided that the following conditions are met:
			* Redistributions of source code must retain the above copyright
			  notice, this list of conditions and the following disclaimer.
			* Redistributions in binary form must reproduce the above copyright
			  notice, this list of conditions and the following disclaimer in the
			  documentation and/or other materials provided with the distribution.
			* Neither the name of the <organization> nor the
			  names of its contributors may be used to endorse or promote products
			  derived from this software without specific prior written permission.

		THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
		ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
		WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
		DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
		DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
		(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
		LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
		ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
		(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
		SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
	</License>
	<Owner Name="Daniel Vaughan" Email="danielvaughan@outcoder.com" />
	<CreationDate>2010-01-31 13:08:07Z</CreationDate>
</File>
*/
#endregion

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Windows;
using System.Windows.Controls;

namespace DanielVaughan.Windows
{
	/// <summary>
	/// Extension methods for <see cref="Collection{ResourceDictionary}"/>.
	/// </summary>
	public static class ResourceDictionaryExtensions
	{
		public static void Add(this Collection<ResourceDictionary> target, 
		                       string path, string assemblyFullName)
		{
			string dictionaryName = string.Format("/{0};component/{1}", assemblyFullName, path);
			var uri = new Uri(dictionaryName, UriKind.Relative);

			/* Silverlight */
//			Application.LoadComponent(Application.Current, uri);
//			target.Add(resourceDictionary);
#if !SILVERLIGHT
			var dictionary = (ResourceDictionary)Application.LoadComponent(uri);
			target.Add(dictionary);
#else
			throw new NotImplementedException();
#endif
		}

#if !SILVERLIGHT
		/// <summary>
		/// Adds the dictionary whose location is specified by the relative pack URI.
		/// An example of such a URI 
		/// is /DanielVaughan.Calcium.Client;component/Gui/Controls/SplashScreen/SplashScreenView.xaml
		/// </summary>
		/// <param name="target">The merged dictionary to add to.</param>
		/// <param name="relativeUrl">The relative URL.</param>
		/// <exception cref="ArgumentException">
		/// Occurs if the specified URL is not relative.</exception>
		/// <exception cref="ArgumentNullException">Occurs if either the target, 
		/// or relativeUrl is <c>null</c>.</exception>
		public static void AddRelativeDictionary(this Collection<ResourceDictionary> target, 
			string relativeUrl)
		{
			ArgumentValidator.AssertNotNull(target, "target");
			ArgumentValidator.AssertNotNull(relativeUrl, "relativeUrl");

			var uri = new Uri(relativeUrl, UriKind.Relative);
			var dictionary = (ResourceDictionary)Application.LoadComponent(uri);
			target.Add(dictionary);
		}

		/// <summary>
		/// Adds dictionaries whose locations are specified by relative pack URIs.
		/// An example of such a URI 
		/// is /DanielVaughan.Calcium.Client;component/Gui/Controls/SplashScreen/SplashScreenView.xaml
		/// </summary>
		/// <param name="target">The merged dictionary to add to.</param>
		/// <param name="relativeUrls">The relative URLs of the resource dictionaries.</param>
		/// <exception cref="ArgumentException">
		/// Occurs if any of the specified URLs are not relative.</exception>
		/// <exception cref="ArgumentNullException">Occurs if either the target, 
		/// or relativeUrls is <c>null</c>.</exception>
		public static void AddRelativeDictionaries(this Collection<ResourceDictionary> target, 
			IEnumerable<string> relativeUrls)
		{
			ArgumentValidator.AssertNotNull(target, "target");
			ArgumentValidator.AssertNotNull(relativeUrls, "relativeUrls");

			foreach (var relativeUrl in relativeUrls)
			{
				target.AddRelativeDictionary(relativeUrl);
			}
		}

		/// <summary>
		/// Adds the dictionary whose location is specified by the relative pack URI.
		/// </summary>
		/// <param name="target">The merged dictionary to add to.</param>
		/// <param name="relativeUri">The relative Uri.</param>
		/// <exception cref="ArgumentException">
		/// Occurs if the specified URI is not relative.</exception>
		/// <exception cref="ArgumentNullException">Occurs if either the target, 
		/// or relativeUri is <c>null</c>.</exception>
		public static void AddRelativeDictionary(this Collection<ResourceDictionary> target,
			Uri relativeUri)
		{
			ArgumentValidator.AssertNotNull(target, "target");
			ArgumentValidator.AssertNotNull(relativeUri, "relativeUri");

			var dictionary = (ResourceDictionary)Application.LoadComponent(relativeUri);
			target.Add(dictionary);
		}

		/// <summary>
		/// Adds dictionaries whose locations are specified by relative pack URIs.
		/// </summary>
		/// <param name="target">The merged dictionary to add to.</param>
		/// <param name="relativeUris">The relative URIs of the resource dictionaries.</param>
		/// <exception cref="ArgumentException">
		/// Occurs if any of the specified URIs are not relative.</exception>
		/// <exception cref="ArgumentNullException">Occurs if either the target, 
		/// or relativeUris is <c>null</c>.</exception>
		public static void AddRelativeDictionaries(this Collection<ResourceDictionary> target,
			IEnumerable<Uri> relativeUris)
		{
			ArgumentValidator.AssertNotNull(target, "target");
			ArgumentValidator.AssertNotNull(relativeUris, "relativeUrls");

			foreach (var uri in relativeUris)
			{
				target.AddRelativeDictionary(uri);
			}
		}
#endif
	}
}